import pandas as pd
import datetime
import os
import numpy as np
from WindPy import w
import Core.Config as Config
import Core.Gadget as Gadget
import Core.Quote as Quote
import matplotlib.pyplot as plt
from SystematicFactors.General import Load_Systematic_Factor


# 打印所有因子的时间区间 info
def Print_Systematic_Factor_List(database, tableName="a_sys_factor"):
    factor_names = database.ExecuteSQL("Factor", "SELECT distinct(Name) FROM " + tableName)
    #
    for factor_name in factor_names:
        factor_name = factor_name[0]
        factor_series = database.Find("Factor", tableName, {"Name": factor_name}, sort=[("Date", 1)])
        datetime1 = factor_series[0]["date"]
        datetime2 = factor_series[-1]["date"]
        print(datetime1, " To ", datetime2, factor_name, "#", len(factor_series))
    #
    print("Total StockFactors", len(factor_names))


# 缺失数据库，作用未知
def Print_Systematic_Factor_Values(database):
    factor_names = database.ExecuteSQL("macro", "SELECT distinct(Name) FROM macro.market_factor", "a_sys_factor_market")
    #
    for factor_name in factor_names:
        factor_name = factor_name[0]
        factor_series = database.Find("macro", "market_factor", {"Name": factor_name}, sort=[("Date", 1)])
        df = Gadget.DocumentsToDataFrame(factor_series, keep=["Date", "Value", "Modified_Time"])
        print(factor_name)
        print(df.tail(10))
    #
    print("Total StockFactors", len(factor_names))


def Print_Systematic_Factor(database, factor_name, datetime1=None, datetime2=None):
    #
    df = Load_Systematic_Factor(database, factor_name, datetime1=datetime1, datetime2=datetime2)
    print(df.describe())
    print(df)
    return df


# 把同类因子merge再打印
def Print_Systematic_FactorValues_Category(database):
    pass


#
def Plot_Systematic_Factor(database, factor_name, datetime1=None, datetime2=None):
    #
    df = Load_Systematic_Factor(database, factor_name, datetime1=datetime1, datetime2=datetime2)
    date_field = "date"
    #
    df.plot(x=date_field, y=[factor_name], grid=True, title=factor_name)
    # ax1.set_ylabel('Net Unit Value')
    print(df.describe())
    print(df)
    plt.show()
    #
    return df


if __name__ == '__main__':
    # ---Connecting Database---
    path_filename = os.getcwd() + "\..\Config\config_local.json"
    database = Config.create_database(database_type="MySQL", config_file=path_filename, config_field="MySQL")

    #
    # Print_Systematic_Factor_List(database)
    # Print_Systematic_Factor_Values(database)

    # 货币现象
    # Plot_Systematic_Factor(database, "CB_ReverseRepo7")

    # Plot_Systematic_Factor(database, "CB_NetInvested_Monthly", datetime.datetime(2015, 1, 1), datetime.datetime(2020, 12, 31))
    # Plot_Systematic_Factor(database, "CB_NetInvested_Weekly", datetime.datetime(2015,1,1), datetime.datetime(2020,12,31))
    # Plot_Systematic_Factor(database, "Money_Multiplier")


    # 货币现象 - 货币市场 FX
    # Plot_Systematic_Factor(database, "USDCNY_PBOC", datetime.datetime(2015,1,1), datetime.datetime(2020,12,31))
    # Plot_Systematic_Factor(database, "USDCNY_Weekly_Return")
    # Plot_Systematic_Factor(database, "USDCNH_USDCNY_Spread")
    # Plot_Systematic_Factor(database, "USDCNH_Term_Spread")

    # 信用现象
    # Plot_Systematic_Factor(database, "Social_Financing_Balance")

    # 经济现象
    # Plot_Systematic_Factor(database, "Investor_Individual")
    # Plot_Systematic_Factor(database, "Liquidity_Surplus")
    # Plot_Systematic_Factor(database, "Industrial_Value_Added_YoY", table_name="a_sys_factor_macro")

    # 周期
    # Plot_Systematic_Factor(database, "Hurst_Weekly")
    # Plot_Systematic_Factor(database, "Hurst_Monthly")
    # Plot_Systematic_Factor(database, "Kitchin_Cycle_Weekly")
    # Plot_Systematic_Factor(database, "Kitchin_Cycle_Monthly")

    # 债券
    # Plot_Systematic_Factor(database, "CorpBond_AAP_YTM_5Y", table_name="a_sys_factor_market")
    # Plot_Systematic_Factor(database, "CorpBond_AAP_YTM_10Y", table_name="a_sys_factor_market")
    # Plot_Systematic_Factor(database, "CorpBond_AAA_YTM_5Y", table_name="a_sys_factor_market")
    # Plot_Systematic_Factor(database, "CorpBond_AAP_YTM_10Y", table_name="a_sys_factor_market")
    # Plot_Systematic_Factor(database, "Credit_Spread_5Y")
    # Plot_Systematic_Factor(database, "Credit_Spread_1Y")

    #
    Plot_Systematic_Factor(database, "Term_Spread_10Y1Y")
    Plot_Systematic_Factor(database, "Term_Spread_10Y1Y_Avg_Monthly")
    Plot_Systematic_Factor(database, "Term_Spread_10Y1Y_Avg_Monthly_Dif")

    #
    # Plot_Systematic_Factor(database, "Shibor_3M", table_name="a_sys_factor_market")
    # Plot_Systematic_Factor(database, "Shibor_3M")

    # 股票-资金流入
    # Plot_Systematic_Factor(database, "MarginTrade_ShortBalance")
    # Plot_Systematic_Factor(database, "MarginTrade_Investor")

    # Plot_Systematic_Factor(database, "Northbound_NetBuy_Aggr")
    # Plot_Systematic_Factor(database, "Northbound_NetBuy_Weekly_Dif")

    # 股票-基本面
    # Plot_Systematic_Factor(database, "PMI", table_name="a_sys_factor_macro")
    # Plot_Systematic_Factor(database, "ROE_Median_TotalA", table_name="a_sys_factor_macro")
    # Plot_Systematic_Factor(database, "ROE_Average_TotalA", table_name="a_sys_factor_macro")

    # 股票-情绪技术
    # Plot_Systematic_Factor(database, "Turnover_TotalA")
    # Plot_Systematic_Factor(database, "Turnover_HS300_Rank")
    # Plot_Systematic_Factor(database, "Turnover_HS300")
    # Plot_Systematic_Factor(database, "Turnover_TotalA")

    # Plot_Systematic_Factor(database, "HS300_Dividend_To_Tbond_Yield_Weekly")
    # Plot_Systematic_Factor(database, "HS300_Dividend_To_Tbond_Yield")
    # Plot_Systematic_Factor(database, "HS300_PE_Chg_To_Amt_Chg_Weekly")

    #
    # Plot_Systematic_Factor(database, "Dividend_Yield_12M_ZZ500")
    # Plot_Systematic_Factor(database, "PE_Level_HS300")

    # Plot_Systematic_Factor(database, "IH_Basis_Time_Weighted")
    # Plot_Systematic_Factor(database, "IF_Basis_Time_Weighted")
    # Plot_Systematic_Factor(database, "IC_Basis_Time_Weighted")
    # Plot_Systematic_Factor(database, "IM_Basis_Time_Weighted")





